package com.syn.synold4s.activity.base;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import androidx.databinding.ViewDataBinding;

import com.gaoyi.basemodule.baseact.BaseActivity;
import com.gaoyi.basemodule.basebean.Event;
import com.gaoyi.basemodule.baseconstant.EventCode;
import com.gaoyi.basemodule.basevm.BaseViewModel;
import com.syn.synold4s.control.dialog.AlertDialog;

public abstract class BaseAlertActivity<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseActivity<V, VM> {
    @Override
    protected void event(Event event) {
        if (event.getWhat() == EventCode.EVENT_ALERT) {
            String currentActivityName = getCurrentActivityName(this);
            if (!TextUtils.isEmpty(currentActivityName) && currentActivityName.equals(getClass().getSimpleName())) {
                new AlertDialog(mContext, event.getWearer(), event.getTel(), event.getAddress(),event.getType(),event.getAlertTime()).show(getSupportFragmentManager());
            }
        }
    }

    public String getCurrentActivityName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String name = cn.getShortClassName();
        return name.substring(name.lastIndexOf(".") + 1);
    }
}
